<?php
class IndexControl extends Control{
	private function vote($oid){
		$log_vote = M('log');
		$uip = Ip::getClientIp();
		$l_votetime = strtotime(date('Y-m-d 00:00:00'));
		$row=$log_vote->where("uip = '$uip' AND addtime > $l_votetime")->count();
		if($row >= 5 || cookie('vote_count') >=5){
			$this->ajax(array('state'=>0,'message'=>'每天投票不能超过5次！'));
			return false;
		}else{
			$lasttime = strtotime(date('Y-m-d 23:59:59'))-NOW;
			if(!cookie('vote_count')){
				cookie('vote_count',1,$lasttime);
			}else{
				cookie('vote_count',cookie('vote_count')+1,$lasttime);
			}
			$log_vote->data['oid'] = $oid;
			$log_vote->data['uip'] = $uip;
			$log_vote->data['addtime'] = NOW;
			return $log_vote->add();
		}
	}
    public function index(){
		$_db = M('object');
		$this->field = $_db->all();
        $this->display();
    }
	public function add_vote(){
		$oid = Q('oid',0);
		if($oid){
			if($this->vote($oid)){
				$obj_db = M('object');
				$vote_obj = $obj_db->find($oid);
				$count = $obj_db->data['count'] = $vote_obj['count']+1;
				$obj_db->data['oid'] = $oid;
				if($obj_db->save()){
					$this->ajax(array('state'=>1,'message'=>'你成功给'.$vote_obj['name'].'投了一票','count'=>$count));
				}else{
					$this->ajax(array('state'=>0,'message'=>'系统错误，请联系管理员QQ864923962'));
				}
			}
		}else{
			$this->ajax(array('state'=>0,'message'=>'传递参数错误'));
		}
        
	}
}
?>